package com.hwg.emp.web.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.model.EmployeeMD;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.EmployeeService;
import com.hwg.emp.service.ServiceFactroy;
import com.hwg.emp.service.UserService;

public class UpdateEmpController extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	req.setCharacterEncoding("utf-8");
    	resp.setCharacterEncoding("utf-8");
    	EmployeeMD update_employee = new EmployeeMD();
    	UserMD update_user = new UserMD();
    	EmployeeService e_service = ServiceFactroy.getService(EmployeeService.class);
    	UserService u_service = ServiceFactroy.getService(UserService.class);
    	String update_account = req.getParameter("account");
    	String update_name = req.getParameter("name");
    	String update_sex = req.getParameter("sex");
    	String sex = URLDecoder.decode(update_sex, "utf-8");
    	String update_age = req.getParameter("age");
    	String update_birth = req.getParameter("birth");
    	String update_phone = req.getParameter("update_phone");
    	
    	try {
    		UserMD user = u_service.select("name", update_name);
			u_service.select("name", update_name);
			if (sex.equals("男")) { 
	    		update_employee.setSex("1");
			}else if (sex.equals("女")) {
				update_employee.setSex("0");
			}
	    	update_employee.setAge(update_age);
	    	update_employee.setBirth(java.sql.Date.valueOf(update_birth));
	    	update_employee.setPhone(update_phone);
	    	update_employee.setId(user.getId());
	    	update_user.setAccount(update_account);
	    	update_user.setName(update_name);
	    	e_service.update(update_employee);
			u_service.updateUserOnfo(update_user);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	req.getRequestDispatcher("/selectEmps.do").forward(req, resp);
    }
}
